﻿using LevelEditor.Model.Entity;
using LevelEditor.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace LevelEditor.Model.IO
{
    public class UIdIO
    {
        // Fields
        public static string IDS_XML = @"Assets\ids.xml";

        // Methods
        public static IDs LoadIDs()
        {
            string path = FileUtil.AppStartUpPath + IDS_XML;
            FileInfo idsXml = new FileInfo(path);
            IDs ids = new IDs();
            if (!idsXml.Exists)
            {
                return ids;
            }
            XmlSerializer serializer = new XmlSerializer(typeof(IDs));
            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                return (serializer.Deserialize(stream) as IDs);
            }
        }

        public static void SaveIDs(IDs ids)
        {
            string path = FileUtil.AppStartUpPath + IDS_XML;
            XmlSerializer serializer = new XmlSerializer(ids.GetType());
            using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                serializer.Serialize((Stream)stream, ids);
            }
        }
    }


}
